package com.company.biz.user.action;

import cn.hutool.core.lang.tree.Tree;
import com.company.biz.user.service.DepartmentService;
import com.company.user.entity.DepartmentEntity;
import com.company.user.entity.UserEntity;
import com.company.user.req.*;
import com.company.user.vo.GeneralResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(description = "部级管理")
@RestController
@RequestMapping("/department")
public class DepartmentController extends BaseController {
    @Resource
    private DepartmentService departmentService;

    @ApiOperation(value = "获取一层的级部")
    @RequestMapping(value = "/getDepartmentList", method = RequestMethod.POST)
    public GeneralResult<List<DepartmentEntity>> getDepartmentList(@RequestBody DepartmentListReq req) {
        List<DepartmentEntity> departmentList = departmentService.getDepartmentList(req);
        return GeneralResult.genSuccessResult(departmentList);
    }

    @ApiOperation(value = "添加公司或者部门")
    @RequestMapping(value = "/addDepartment", method = RequestMethod.POST)
    public GeneralResult<String> addDepartment(@RequestBody AddDepartmentReq req) {
        boolean b = departmentService.addDepartment(req);
        return b ? GeneralResult.genSuccessResult("添加成功") : GeneralResult.genErrorResult("添加失败");
    }

    @ApiOperation(value = "通过部门id获取部门信息")
    @RequestMapping(value = "/getDepartmentInfoByDepartmentId", method = RequestMethod.POST)
    public GeneralResult<DepartmentEntity> getDepartmentInfoByDepartmentId(@RequestBody DepartmentIdReq req) {
        UserEntity userEntity = getUserEntity();
        DepartmentEntity departmentEntity = departmentService.getDepartmentInfoByDepartmentId(req.getDepartmentId());
        return GeneralResult.genSuccessResult(departmentEntity);
    }

    @ApiOperation(value = "通过部门id修改部门信息")
    @RequestMapping(value = "/updateDepartmentInfoByDepartmentId", method = RequestMethod.POST)
    public GeneralResult<String> updateDepartmentInfoByDepartmentId(@RequestBody DepartmentInfoReq req) {
        boolean b = departmentService.updateDepartmentInfoByDepartmentId(req);
        return b ? GeneralResult.genSuccessResult("修改成功") : GeneralResult.genErrorResult("修改失败");
    }

    @ApiOperation(value = "通过部门id删除部门信息")
    @RequestMapping(value = "/deleteDepartmentByDepartmentId", method = RequestMethod.POST)
    public GeneralResult<String> deleteDepartmentByDepartmentId(@RequestBody DepartmentIdReq req) {
        boolean b = departmentService.deleteDepartmentByDepartmentId(req.getDepartmentId());
        return b ? GeneralResult.genSuccessResult("删除成功") : GeneralResult.genErrorResult("删除失败");
    }

    @ApiOperation("所有部门的部门树")
    @PostMapping("/getAllDepartmentTree")
    public GeneralResult<List<Tree<String>>> getAllDepartmentTree() {
        UserEntity userEntity = getUserEntity();
        //获取
        List<Tree<String>> departmentList = departmentService.getAllDepartmentTree();
        //判断
        return GeneralResult.genSuccessResult(departmentList);

    }


}
